package com.qing.utils.lottery.dlt.analyse;

import java.util.Arrays;
import java.util.List;

public class TwoSectionAnalyser extends Analyser {

    public static final int RED_BIG_BEGIN = 19;
    public static final int RED_BIG_END = 35;
    public static final int BLUE_BIG_BEGIN = 7;
    public static final int BLUE_BIG_END = 12;
    private static final int RED_SMALL_BEGIN = 1;
    private static final int RED_SMALL_END = 18;
    private static final int[] RED_BIG_SECTION = new int[RED_BIG_END - RED_BIG_BEGIN + 1];
    private static final int[] RED_SMALL_SECTION = new int[RED_SMALL_END - RED_SMALL_BEGIN + 1];
    private static final int BLUE_SMALL_BEGIN = 1;
    private static final int BLUE_SMALL_END = 6;
    private static final int[] BLUE_BIG_SECTION = new int[BLUE_BIG_END - BLUE_BIG_BEGIN + 1];
    private static final int[] BLUE_SMALL_SECTION = new int[BLUE_SMALL_END - BLUE_SMALL_BEGIN + 1];


    static {
        for (int i = RED_SMALL_BEGIN; i <= RED_SMALL_END; i++) {
            RED_SMALL_SECTION[i - 1] = i;
        }

        for (int i = RED_BIG_BEGIN; i <= RED_BIG_END; i++) {
            RED_BIG_SECTION[i - RED_BIG_BEGIN] = i;

        }
        for (int i = BLUE_SMALL_BEGIN; i <= BLUE_SMALL_BEGIN; i++) {
            BLUE_BIG_SECTION[i - 1] = i;
        }

        for (int i = BLUE_BIG_BEGIN; i <= BLUE_BIG_END; i++) {
            BLUE_BIG_SECTION[i - BLUE_BIG_BEGIN] = i;

        }
    }

    public AnalyserResult analyse(AnalyserParam param) {
        AnalyserResult result = new AnalyserResult();
        result.setRedSectionList(analyseRed(param.getRedValues()));
        result.setBlueSectionList(analyseBlue(param.getBlueValues()));

        return result;
    }

    protected List<AnalyserResult.ResultSection> analyseRed(int[] redValues) {
        int[] redSmallSection = TwoSectionAnalyser.RED_SMALL_SECTION;
        int redSmallCount = 0;
        int[] redBigSection = TwoSectionAnalyser.RED_BIG_SECTION;
        int redBigCount = 0;
        for (int redValue : redValues) {
            if (analyseRedSmall(redValue)) {
                redSmallCount++;
            } else {
                redBigCount++;
            }
        }

        return Arrays.asList(new AnalyserResult.ResultSection(redSmallSection, redSmallCount), new AnalyserResult.ResultSection(redBigSection, redBigCount));
    }

    protected List<AnalyserResult.ResultSection> analyseBlue(int[] blueValues) {
        int[] redSmallSection = TwoSectionAnalyser.BLUE_SMALL_SECTION;
        int redSmallCount = 0;
        int[] redBigSection = TwoSectionAnalyser.BLUE_BIG_SECTION;
        int redBigCount = 0;
        for (int redValue : blueValues) {
            if (analyseRedSmall(redValue)) {
                redSmallCount++;
            } else {
                redBigCount++;
            }
        }

        return Arrays.asList(new AnalyserResult.ResultSection(redSmallSection, redSmallCount), new AnalyserResult.ResultSection(redBigSection, redBigCount));
    }

    private boolean analyseRedSmall(int redValue) {

        for (int i : RED_SMALL_SECTION) {
            if (i == redValue) {
                return true;
            }
        }
        return false;
    }

    private boolean analyseRedBig(int redValue) {
        for (int i : RED_BIG_SECTION) {
            if (i == redValue) {
                return true;
            }
        }
        return false;
    }


    private boolean analyseBlueSmall(int blueValue) {
        for (int i : RED_SMALL_SECTION) {
            if (i == blueValue) {
                return true;
            }
        }
        return false;

    }

    private boolean analyseBlueBig(int blueValue) {
        for (int i : RED_BIG_SECTION) {
            if (i == blueValue) {
                return true;
            }
        }
        return false;
    }


}
